package com.ruoyi.zsyh.controller.applet.user;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.zsyh.domain.*;
import com.ruoyi.zsyh.service.*;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 个人单
 */
@Controller
@RequestMapping("/applet/zsyh/personOrder")
public class ZsyhPersonOrderAppletController extends BaseController
{

    @Autowired
    private IZsyhPersonOrderService zsyhPersonOrderService;

    @Autowired
    private IZsyhUserRepairareaService zsyhUserRepairareaService;

    @Autowired
    private IZsyhServiceTypeMoneyService zsyhServiceTypeMoneyService;

    @Autowired
    private IZsyhServiceTypeMoneyotherService zsyhServiceTypeMoneyotherService;

    @Autowired
    private IZsyhServiceTypeService zsyhServiceTypeService;

    /**
     * 发布服务需求 -- 个人单
     * @return
     * @throws Exception
     */
    @RepeatSubmit
    @RequestMapping("/savePersonOrder")
    @ResponseBody
    public AjaxResult saveServiceOrder(@RequestBody ZsyhPersonOrder personOrder)throws Exception
    {

        personOrder.setId(IdWorker.getId());
        personOrder.setOrderState("0");
        if(StringUtils.isNotEmpty(personOrder.getFaultComment())){
            personOrder.setFaultComment(personOrder.getFaultComment());
        }else{
            personOrder.setFaultComment("");
        }
        String rakerId = personOrder.getRakerId();
        if(EmptyUtils.isNotEmpty(rakerId)){
            //扫码订单
            ZsyhUserRepairarea area = new ZsyhUserRepairarea();
            area.setRepairId(rakerId);
            List<ZsyhUserRepairarea> areaList = zsyhUserRepairareaService.selectZsyhUserRepairareaList(area);
            String orderQrcodeArea = "";
            for(ZsyhUserRepairarea a:areaList){
                String district = a.getRepairDistrict();
                orderQrcodeArea += district + ",";
            }
            personOrder.setOrderQrcodeArea(orderQrcodeArea);
        }else{
            //散户订单
            ZsyhUserRepairarea repairarea = zsyhUserRepairareaService.selectAgentIdByAreaId(personOrder.getOrderDistrict());
            if(repairarea != null){
                personOrder.setRakerId(repairarea.getRepairId());
                personOrder.setAgentId(repairarea.getAgentId());
            }
        }

        if(EmptyUtils.isNotEmpty(personOrder.getRakerId())){//有网点id 保存为扫码单
            personOrder.setOrderType("2");
        }else{//个人单 保存订单的固定抽成费用
            String serviceTypeId = personOrder.getServiceTypeId();
            ZsyhServiceType type = zsyhServiceTypeService.selectZsyhServiceTypeById(serviceTypeId);
            personOrder.setOrderFixdraw(type.getServiceFixdraw());
        }

        zsyhPersonOrderService.insertZsyhPersonOrder(personOrder);

        return success("操作成功");
    }

    /**
     * 下单时获取收费标准
     * @return
     */
    @RequestMapping("/getTypeMoneyList")
    @ResponseBody
    public AjaxResult getTypeMoneyList(String rakerId, String secondTypeId, String thirdTypeId, String key)throws Exception
    {
        ZsyhServiceTypeMoney money = new ZsyhServiceTypeMoney();
        if(StringUtils.isNotEmpty(rakerId)){
            money.setUserStoreId(rakerId);//市县为空 查询时候传0000.否则会全部查出来
        }else{
            money.setUserStoreId("0000000");
        }
        money.setServiceTypeId(thirdTypeId);
        money.setName(key);
        List<ZsyhServiceTypeMoney> moneyList = zsyhServiceTypeMoneyService.selectZsyhServiceTypeMoneyList(money);

        ZsyhServiceTypeMoneyother moneyother = new ZsyhServiceTypeMoneyother();
        if(StringUtils.isNotEmpty(rakerId)){//市县为空 查询时候传0000.否则会全部查出来
            moneyother.setUserStoreId(rakerId);
        }else{
            moneyother.setUserStoreId("0000000");
        }
        moneyother.setServiceTypeId(secondTypeId);
        moneyother.setName(key);
        List<ZsyhServiceTypeMoneyother> moneyOtherList = zsyhServiceTypeMoneyotherService.selectZsyhServiceTypeMoneyotherList(moneyother);

        Map map = new HashMap<>();
        map.put("moneyList", moneyList);
        map.put("moneyOtherList", moneyOtherList);
        return success("成功", map);
    }



    /**
     * 获取服务订单列表
     * @return
     */
    @RequestMapping("/getPersonOrderListByUserId")
    @ResponseBody
    public AjaxResult getPersonOrderListByUserId(String userId, String orderState, String pageNum, String pageSize)throws Exception
    {
        ZsyhPersonOrder order = new ZsyhPersonOrder();
        if(EmptyUtils.isNotEmpty(orderState)){
            order.setOrderState(orderState);
        }
        order.setUserId(userId);
        Map map = new HashedMap();
        if(EmptyUtils.isNotEmpty(pageNum) && EmptyUtils.isNotEmpty(pageSize)){
            int num = Integer.valueOf(pageNum);
            int size = Integer.valueOf(pageSize);
            num = (num - 1) * size;
            map.put("beginPos", num);
            map.put("pageSize", size);
        }
        map.put("orderState", "未取消");
        order.setParams(map);
        List<ZsyhPersonOrder> orderList = zsyhPersonOrderService.selectZsyhPersonOrderList(order);
        return success("成功", orderList);
    }

    /**
     * 获取服务订单详情，电子保单
     * @return
     */
    @RequestMapping("/getPersonOrderDetailsById")
    @ResponseBody
    public AjaxResult getPersonOrderDetailsById(String orderId)throws Exception
    {
        ZsyhPersonOrder order = zsyhPersonOrderService.selectZsyhPersonOrderById(orderId);
        return success("成功", order);
    }

    /**
     * 取消订单
     * @param orderId
     * @return
     * @throws Exception
     */
    @RequestMapping("/cancelPersonOrder")
    @ResponseBody
    public AjaxResult cancelPersonOrder(String orderId)throws Exception
    {
        ZsyhPersonOrder order = new ZsyhPersonOrder();
        order.setId(orderId);
        order.setOrderState("5");
        zsyhPersonOrderService.updateZsyhPersonOrderApplet(order);
        return success("成功");
    }
}
